package org.dong.control;

import org.dong.pojo.Dto;
import org.dong.pojo.Meal;
import org.dong.service.MealService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MealControl {


    @Resource
    MealService mealService;
    @RequestMapping("Meal/getList")
    public Dto getMealList(HttpServletRequest req, HttpServletResponse resp){
        Dto dto = new Dto();
        Integer page = Integer.valueOf(req.getParameter("page"));

        if(page < 0){
            page =  0;
        }else{
            page = page *5;
        }

        List<Meal> mealList = mealService.getMealList(page);
        Integer mealListCount = mealService.getMealListCount();

        Map<String, Object> data = new HashMap<String, Object>();
                data.put("list", mealList);
                data.put("count", mealListCount);
                dto.setDatas(data);
                dto.setErrMsg("套餐列表");
                dto.setStatusCode(200);

        return dto;
    }


    public Dto getMealInfo(HttpServletRequest req,HttpServletResponse resp){
        Dto dto = new Dto();
        Integer id = Integer.valueOf(req.getParameter("id"));
        Meal meal = mealService.getMeal(id);

        if(meal != null){
            Map<String, Object> data = new HashMap<String, Object>();

                data.put("info", meal);
                dto.setDatas(data);
                dto.setErrMsg("套餐详情");
                dto.setStatusCode(200);
        }else{
                dto.setErrMsg("套餐不存在");
                dto.setStatusCode(2001);
        }

        return dto;
    }
}
